package com.powerbank.adminsystem.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.powerbank.adminsystem.entity.AdminMenu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 菜单Mapper接口
 */
@Mapper
public interface AdminMenuMapper extends BaseMapper<AdminMenu> {

    /**
     * 根据父级ID查询子菜单
     */
    @Select("SELECT * FROM admin_menu WHERE parent_id = #{parentId} AND status = 1 AND deleted = 0 ORDER BY sort")
    List<AdminMenu> findByParentId(@Param("parentId") Long parentId);

    /**
     * 根据角色ID查询菜单列表
     */
    @Select("SELECT m.* FROM admin_menu m " +
            "JOIN admin_role_menu rm ON m.id = rm.menu_id " +
            "WHERE rm.role_id = #{roleId} AND m.status = 1 AND m.deleted = 0 AND rm.deleted = 0 " +
            "ORDER BY m.sort")
    List<AdminMenu> findMenusByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据用户ID查询菜单列表
     */
    @Select("SELECT DISTINCT m.* FROM admin_menu m " +
            "JOIN admin_role_menu rm ON m.id = rm.menu_id " +
            "JOIN admin_user_role ur ON rm.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND m.status = 1 AND m.deleted = 0 AND rm.deleted = 0 AND ur.deleted = 0 " +
            "ORDER BY m.sort")
    List<AdminMenu> findMenusByUserId(@Param("userId") Long userId);

    /**
     * 根据权限标识查询菜单
     */
    @Select("SELECT * FROM admin_menu WHERE permission = #{permission} AND deleted = 0")
    AdminMenu findByPermission(@Param("permission") String permission);
}